package com.lepu.app.fun.tool.bbwy;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.utils.LoginUtil;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.ApiClient;
import com.core.lib.core.AsyncRequest;
import com.core.lib.ui.wheel.WheelView;
import com.core.lib.utils.main.AppManager;
import com.core.lib.utils.main.DateUtilBase;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.widget.MyDialog;
import com.eyzhs.app.R;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;

public class BbwyMainChouChouActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener, AsyncRequest {

	private static final String REQUEST_FINISH = "request_finish";

	int mShitType = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		mNeedStatusBar = false;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bbwy_activity_chou_chou);

		UtilityBase.setStatusBarRes(this, R.color.color_bbwy, true);
		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("臭臭");
		topbar.setonTopbarNewLeftLayoutListener(this);
		topbar.setTopbarBackground(R.color.color_bbwy);

		TextView chouchouTimeTextView = (TextView) findViewById(R.id.chouchouTimeTextView);
		String currentDate = DateUtilBase.stringFromDate(new Date(),
				DateUtilBase.DATE_ALL);
		chouchouTimeTextView.setText(currentDate);

		ImageView threeImageView = (ImageView) findViewById(R.id.threeImageView);
		viewClick(threeImageView);
	}

	public void viewClick(View view) {
		int rid = view.getId();

		switch (rid) {
		case R.id.oneImageView:
		case R.id.twoImageView:
		case R.id.threeImageView:
		case R.id.fourImageView:
		case R.id.fiveImageView:

			ImageView oneImageView = (ImageView) findViewById(R.id.oneImageView);
			ImageView twoImageView = (ImageView) findViewById(R.id.twoImageView);
			ImageView threeImageView = (ImageView) findViewById(R.id.threeImageView);
			ImageView fourImageView = (ImageView) findViewById(R.id.fourImageView);
			ImageView fiveImageView = (ImageView) findViewById(R.id.fiveImageView);

			TextView oneTextView = (TextView) findViewById(R.id.oneTextView);
			TextView twoTextView = (TextView) findViewById(R.id.twoTextView);
			TextView threeTextView = (TextView) findViewById(R.id.threeTextView);
			TextView fourTextView = (TextView) findViewById(R.id.fourTextView);
			TextView fiveTextView = (TextView) findViewById(R.id.fiveTextView);

			oneImageView.setSelected(false);
			twoImageView.setSelected(false);
			threeImageView.setSelected(false);
			fourImageView.setSelected(false);
			fiveImageView.setSelected(false);

			oneTextView.setSelected(false);
			twoTextView.setSelected(false);
			threeTextView.setSelected(false);
			fourTextView.setSelected(false);
			fiveTextView.setSelected(false);

			if (rid == R.id.oneImageView) {
				oneImageView.setSelected(true);
				oneTextView.setSelected(true);
				mShitType = 1;
			} else if (rid == R.id.twoImageView) {
				twoImageView.setSelected(true);
				twoTextView.setSelected(true);
				mShitType = 2;
			} else if (rid == R.id.threeImageView) {
				threeImageView.setSelected(true);
				threeTextView.setSelected(true);
				mShitType = 3;
			} else if (rid == R.id.fourImageView) {
				fourImageView.setSelected(true);
				fourTextView.setSelected(true);
				mShitType = 4;
			} else if (rid == R.id.fiveImageView) {
				fiveImageView.setSelected(true);
				fiveTextView.setSelected(true);
				mShitType = 5;
			}
			break;
		case R.id.chouchouTimeTextView:

			showChouChouTime();
			break;
		case R.id.finishButton:

			submit();
			break;

		default:
			break;
		}
	}

	private void submit() {

		String chouchouTime = "";
		TextView chouchouTimeTextView = (TextView) findViewById(R.id.chouchouTimeTextView);
		chouchouTime = chouchouTimeTextView.getText().toString();

		if (TextUtils.isEmpty(chouchouTime)) {
			UIHelper.showToast(this, "请选择喂奶时间");
			return;
		}

		Map<String, Object> map = new HashMap<String, Object>();
		JSONObject params = new JSONObject();
		try {
			params.put("DeviceID", Setting.DEVICEID);
			params.put("ApplicationID", Setting.APPLICATIONID);
			params.put("UserID", LoginUtil.getUserId());
			params.put("LoginToken", LoginUtil.getLoginToken());

			params.put("RecordType", 2);

			params.put("ShitTime", chouchouTime);
			params.put("ShitStatus", mShitType);

		} catch (Exception e) {
		}
		map.put("ht", params);

		ApiClient.http_post_main(Setting.getSaveBabyFeedingRecordUrl(), map,
				null, this, REQUEST_FINISH, true);
	}

	private void showChouChouTime() {
		new MyDialog(this)
				.setTitle("请选择臭臭时间")
				.setDateFullTimePicker(new Date())
				.setNegativeButton(R.string.app_cancel,
						new View.OnClickListener() {

							@Override
							public void onClick(View v) {

							}
						})
				.setPositiveButton(R.string.app_ok, new View.OnClickListener() {

					@Override
					public void onClick(View v) {

						// 如果是个数,则显示为"02"的样式
						WheelView wv_year = (WheelView) v
								.findViewById(R.id.yearFull);
						WheelView wv_month = (WheelView) v
								.findViewById(R.id.monthFull);
						WheelView wv_day = (WheelView) v
								.findViewById(R.id.dayFull);

						WheelView wv_hour = (WheelView) v
								.findViewById(R.id.hourFull);
						WheelView wv_minute = (WheelView) v
								.findViewById(R.id.minuteFull);

						String parten = "00";
						DecimalFormat decimal = new DecimalFormat(parten);
						// 设置日期的显示
						String text = (wv_year.getCurrentItem() + MyDialog.START_YEAR)
								+ "-"
								+ decimal.format((wv_month.getCurrentItem() + 1))
								+ "-"
								+ decimal.format((wv_day.getCurrentItem() + 1));
						String time = decimal.format((wv_hour.getCurrentItem()))
								+ ":"
								+ decimal.format((wv_minute.getCurrentItem()));

						String date = text + " " + time;

						Date selectDate = DateUtilBase.dateFromString(date,
								DateUtilBase.DATE_ALL);
						long selectTime = selectDate.getTime();
						Date currentDate = DateUtilBase.dateFromString(
								DateUtilBase.stringFromDate(new Date(),
										DateUtilBase.DATE_ALL),
								DateUtilBase.DATE_ALL);
						long currentTime = currentDate.getTime();

						if (selectTime > currentTime) {
							UIHelper.showToast(BbwyMainChouChouActivity.this,
									R.string.bbwy_sleep_tip_3);
							return;
						}

						TextView chouchouTimeTextView = (TextView) findViewById(R.id.chouchouTimeTextView);
						chouchouTimeTextView.setText(date);
					}
				}).create(null).show();
	}

	public void RequestComplete(Object object, Object data) {
		if (object.equals(REQUEST_FINISH)) {
			hideProgressDialog();

			finish(true);

			BbwyMainActivity instance = (BbwyMainActivity) AppManager
					.getAppManager().getActivity(BbwyMainActivity.class);
			if (instance != null) {
				instance.request(false);
			}
		}
	}

	@Override
	public void RequestError(Object object, int errorId, String errorMessage) {
		if (object.equals(REQUEST_FINISH)) {
			hideProgressDialog();
		}
	}

}
